﻿using EfCore.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EfCore.Web.ViewComponents
{
    public class ProvinceCitiesCountViewComponent
        : ViewComponent
    {
        private readonly ApplicationContext _ctx;
        public ProvinceCitiesCountViewComponent(ApplicationContext ctx)
        {
            _ctx = ctx;
        }
        public async Task<IViewComponentResult> InvokeAsync(int provinceId)
        {
            var province =await _ctx.Province.AsNoTracking()
                 .Where(p => p.Id == provinceId)
                 .Include(p => p.Cities)
                 .SingleOrDefaultAsync();
            var count = province.Cities.Count;

            return View(count);
        }
    }
}
